package com.san.metadata.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.san.base.dao.BaseManager;
import com.san.common.datasource.JdbcManager;
import com.san.common.utils.Constant;
import com.san.common.utils.StringUtils;
import com.san.metadata.domain.BaseNaturalPersonDO;
import com.san.metadata.vo.BaseNaturalPersonVO;

/**
 * 自然人人基础辨识表JDBC访问层
 * 
 * @author xiangyz
 *
 */
@Service
public class BaseNaturalPersonManager extends BaseManager {

	/**
	 * 根据姓名/身份证号码获取自然人基础辨识信息
	 * 
	 * @param name
	 * @param uscc
	 * @return
	 * @throws Exception
	 */
	public List<BaseNaturalPersonDO> getNaturalPerson(String name, String idCard) throws Exception {
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(idCard)) {
			return new ArrayList<BaseNaturalPersonDO>();
		}
		JdbcManager jdbcManager = new JdbcManager();

		List<Map<String, Object>> result = null;
		String querySQL = "select auto_id,natural_person_id,name,id_card from base_natural_person where name = ? and id_card = ?";

		result = jdbcManager.executeQuery(this.getDBConnection(Constant.SOURCE_CODE_BASESOURCE),querySQL, new Object[] { name, idCard });

		List<BaseNaturalPersonDO> resultList = new ArrayList<BaseNaturalPersonDO>();
		if (null != result) {
			for (Map<String, Object> resultMap : result) {
				BaseNaturalPersonDO baseNaturalPersonDO = new BaseNaturalPersonDO();
				baseNaturalPersonDO.setAutoId((Long) resultMap.get("auto_id"));
				baseNaturalPersonDO.setNaturalPersonId((String) resultMap.get("natural_person_id"));
				baseNaturalPersonDO.setName((String) resultMap.get("name"));
				baseNaturalPersonDO.setIdCard((String) resultMap.get("id_card"));
				resultList.add(baseNaturalPersonDO);
			}
		}

		return resultList;
	}

	/**
	 * 批量保存法人基础辨识信息
	 * 
	 * @param list
	 * @return
	 */
	public int batchSave(List<BaseNaturalPersonVO> list) throws Exception {
		JdbcManager jdbcManager = new JdbcManager();

		try {
			String insertSQL = "insert into base_natural_person(natural_person_id,name,id_card) value(?,?,?)";
			List<Object[]> addList = new ArrayList<Object[]>();
			for (BaseNaturalPersonVO baseNaturalPersonVO : list) {
				addList.add(new Object[] { baseNaturalPersonVO.getNaturalPersonId(), baseNaturalPersonVO.getName(),
						baseNaturalPersonVO.getIdCard() });
			}
			jdbcManager.batchExecuteSQL(this.getDBConnection(Constant.SOURCE_CODE_BASESOURCE),insertSQL, addList);
		} catch (Exception e) {
			throw e;
		}

		return list.size();
	}
}
